/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package engine;

import DataModel.NotaryEntry;
import TransportLayer.IServiceListener;
import TransportLayer.NotaryRequestPacket;
import TransportLayer.NotaryResponse;
import TransportLayer.NotaryResponseType;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import Repository.IRepositoryProxy;

class VerificationHandler implements IRequestHandler
{

	IRepositoryProxy repositoryProxy;
	IServiceListener serviceListener;

	public VerificationHandler( IRepositoryProxy repositoryProxy, IServiceListener serviceListener )
	{
		this.repositoryProxy = repositoryProxy;
		this.serviceListener = serviceListener;
	}

	@Override
	public void handleRequest( NotaryRequestPacket request )
	{
		NotaryEntry entry = repositoryProxy.getEntry(request.getNotaryRequest().getNotaryCertificate().getName());
		NotaryResponse response = new NotaryResponse(NotaryResponseType.OK_Verification, entry.getNotaryCertificate());
		try
		{
			serviceListener.sendResponse(response, request.getSourceAddress(), request.getSourcePort());
		}
		catch ( IOException ex )
		{
			Logger.getLogger(VerificationHandler.class.getName()).log(Level.SEVERE, null, ex);
		}
		catch ( ClassNotFoundException ex )
		{
			Logger.getLogger(VerificationHandler.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
}
